package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.InspectionReportBean;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.hxkcloud.nurse.viewholders.InspectionReportViewholder;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-07-14.
 * Description:检查报告
 */
public class InspectionReportAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<InspectionReportBean> datas;
    private int screenWidth;
    private int picWidth;
    private LayoutInflater mInflater;

    private reportListener listener;

    private ImageLoader imageLoader;


    public InspectionReportAdapter(Context mContext, List<InspectionReportBean> datas, int screenWidth,
                                   reportListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        this.screenWidth = screenWidth;
        this.listener = listener;
        this.picWidth = (screenWidth - ImageUtil.dip2px(60, mContext)) / 3;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();

        handler = new Handler();
    }

    public void setData(List<InspectionReportBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void cleanData() {
        datas.clear();
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_inspectionreport, null);
        InspectionReportViewholder holder = new InspectionReportViewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final InspectionReportViewholder mHolder = (InspectionReportViewholder) holder;
        InspectionReportBean bean = datas.get(position);

        mHolder.tv_name.setText(bean.getTitle());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date(Long.valueOf(bean.getDate())));

        mHolder.tv_date.setText(date);
        mHolder.tv_words.setText(bean.getWords());
        if (bean.getImage() == null || bean.getImage().size() == 0) {
            mHolder.ll_images.setVisibility(View.GONE);
        } else {
            mHolder.ll_images.setVisibility(View.VISIBLE);
            mHolder.iv9.setVisibility(View.GONE);
            mHolder.iv8.setVisibility(View.GONE);
            mHolder.iv7.setVisibility(View.GONE);
            mHolder.iv6.setVisibility(View.GONE);
            mHolder.iv5.setVisibility(View.GONE);
            mHolder.iv4.setVisibility(View.GONE);
            mHolder.iv3.setVisibility(View.GONE);
            mHolder.iv2.setVisibility(View.GONE);
            mHolder.iv1.setVisibility(View.GONE);

            LinearLayout.LayoutParams lp_img = new LinearLayout.LayoutParams(picWidth, picWidth);
            LinearLayout.LayoutParams lp_img1 = new LinearLayout.LayoutParams(picWidth, picWidth);
            lp_img1.leftMargin = ImageUtil.dip2px(5, mContext);
            mHolder.iv1.setLayoutParams(lp_img);
            mHolder.iv2.setLayoutParams(lp_img1);
            mHolder.iv3.setLayoutParams(lp_img1);
            mHolder.iv4.setLayoutParams(lp_img);
            mHolder.iv5.setLayoutParams(lp_img1);
            mHolder.iv6.setLayoutParams(lp_img1);
            mHolder.iv7.setLayoutParams(lp_img);
            mHolder.iv8.setLayoutParams(lp_img1);
            mHolder.iv9.setLayoutParams(lp_img1);
            switch (Math.min(bean.getImage().size(), 9)) {
                case 9:
                    mHolder.iv9.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(8), mHolder.iv9, ImageOptionUtils.getInstance().getSimplePicOptions());
                case 8:
                    mHolder.iv8.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(7), mHolder.iv8, ImageOptionUtils.getInstance().getSimplePicOptions());
                case 7:
                    mHolder.iv7.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(6), mHolder.iv7, ImageOptionUtils.getInstance().getSimplePicOptions());
                case 6:
                    mHolder.iv6.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(5), mHolder.iv6, ImageOptionUtils.getInstance().getSimplePicOptions());
                case 5:
                    mHolder.iv5.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(4), mHolder.iv5, ImageOptionUtils.getInstance().getSimplePicOptions());
                case 4:
                    mHolder.iv4.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(3), mHolder.iv4, ImageOptionUtils.getInstance().getSimplePicOptions());
                case 3:
                    mHolder.iv3.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(2), mHolder.iv3, ImageOptionUtils.getInstance().getSimplePicOptions());
                case 2:
                    mHolder.iv2.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(1), mHolder.iv2, ImageOptionUtils.getInstance().getSimplePicOptions());
                case 1:
                    mHolder.iv1.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(bean.getImage().get(0), mHolder.iv1, ImageOptionUtils.getInstance().getSimplePicOptions());
                    break;
            }
        }

        mHolder.iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 0);
            }
        });
        mHolder.iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 1);
            }
        });
        mHolder.iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 2);
            }
        });
        mHolder.iv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 3);
            }
        });
        mHolder.iv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 4);
            }
        });
        mHolder.iv6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 5);
            }
        });
        mHolder.iv7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 6);
            }
        });
        mHolder.iv8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 7);
            }
        });
        mHolder.iv9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgShow(position, 8);
            }
        });

        if (!TextUtils.isEmpty(bean.getVoice())) {
            mHolder.ll_voice.setVisibility(View.VISIBLE);
            mHolder.ll_voice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onVoice(position);
                }
            });
            if (bean.isVoicePlaying()) {
                AnimationDrawable animDrawable = (AnimationDrawable) ContextCompat.getDrawable(mContext, R.anim.anim_voice);
                animDrawable.start();
                mHolder.iv_voice.setImageDrawable(animDrawable);

            } else {
                mHolder.iv_voice.setImageResource(R.drawable.report_voice);
            }
        } else {
            mHolder.ll_voice.setVisibility(View.GONE);
        }
        mHolder.tv_voice.setVisibility(View.GONE);

        mHolder.ll_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onModify(position);
            }
        });
    }

    private Handler handler;

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface reportListener {
        void onModify(int position);//点击头部去修改

        void onImgShow(int position, int index);//点击查看图片

        void onVoice(int position);//听录音
    }
}
